<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class Activity extends Base
{

	function __construct()
	{
	}

	public function create($param)
	{
		$title = isset($param['title']) ? trim($param['title']):'';
		$pic = isset($param['pic']) ? trim($param['pic']):'';
		$link = isset($param['link']) ? trim($param['link']):'';
		$type= isset($param['type']) ? intval($param['type']):0;
		$content = isset($param['content']) ? trim($param['content']):'';
		$sid = isset($param['sid']) ? intval($param['sid']):0;
		if (!$sid || !$title) {
		//if (!$sid || !$title || !$pic) {
			$this->res_data['Msg'] = "缺少必填信息";
			return $this->res_data;
		}
		if ($type && !$link) {
			$this->res_data['Msg'] = "缺少必填信息";
			return $this->res_data;
		}
    $store= Db::name('store')->where([['id','=',$sid]])->find();
    if (!$store) {
      $this->res_data['Msg'] = "无此门店";
      return $this->res_data;
    }

		$data['title'] =$title;
		$data['sid'] =$sid;
		$data['pic'] =$pic;
		$data['link'] =$link;
		$data['type'] =$type;
		$data['content'] =$content;
		$data['createtime'] =time();
		$data['updatetime'] =time();
    $adata = Db::name('activity')->insertGetId($data);
    if(!$adata) {
        $this->res_data['Msg'] = '添加活动失败';
        return $this->res_data;
    }
    $this->res_data['Msg'] = '成功';
    $this->res_data['Code'] = 0;
    return $this->printResult($this->res_data);
	}
	public function edit($param)
	{
		$title = isset($param['title']) ? trim($param['title']):'';
		$pic = isset($param['pic']) ? trim($param['pic']):'';
		$link = isset($param['link']) ? trim($param['link']):'';
		$type= isset($param['type']) ? intval($param['type']):0;
		$content = isset($param['content']) ? trim($param['content']):'';
		$aid = isset($param['aid']) ? intval($param['aid']):0;
		$sid = isset($param['sid']) ? intval($param['sid']):0;
		//if (!$aid || !$title || !$pic || !$sid) {
		if (!$aid || !$title || !$sid) {
			$this->res_data['Msg'] = "缺少必填信息";
			return $this->res_data;
		}
    $store= Db::name('store')->where([['id','=',$sid]])->find();
    if (!$store) {
      $this->res_data['Msg'] = "无此门店";
      return $this->res_data;
    }
		if ($type && !$link) {
			$this->res_data['Msg'] = "缺少必填信息";
			return $this->res_data;
		}
    $act= Db::name('activity')->where([['id','=',$aid],['sid','=',$sid]])->find();
    if (!$act) {
      $this->res_data['Msg'] = "无此活动";
      return $this->res_data;
    }

		$data['title'] =$title;
		$data['pic'] =$pic;
		$data['link'] =$link;
		$data['type'] =$type;
		$data['content'] =$content;
		$data['updatetime'] =time();
    $adata = Db::name('activity')->where([['id','=',$aid]])->data($data)->update();
    if(!$adata) {
        $this->res_data['Msg'] = '编辑活动失败';
        return $this->res_data;
    }
    $this->res_data['Msg'] = '成功';
    $this->res_data['Code'] = 0;
    return $this->printResult($this->res_data);
	}

}

?>
